package com.sundi.ecsystem.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.eccommon.dto.EcUserDto;
import com.sundi.eccommon.utils.ExcelBean;
import com.sundi.eccommon.utils.ExcelUtils;
import com.sundi.ecsystem.entity.EcUser;
import com.sundi.ecsystem.mapper.EcUserMapper;
import com.sundi.ecsystem.service.IEcUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mht
 * @since 2020-02-24
 */
@Service
public class EcUserServiceImpl extends ServiceImpl<EcUserMapper, EcUser> implements IEcUserService {

    @Override
    public XSSFWorkbook downLoadGoodsExcel(EcUserDto ecUserDto) throws Exception {

        PageHelper.startPage(ecUserDto.getPageNum(),ecUserDto.getPageSize());
        Wrapper wrapper = new EntityWrapper();

        if (StringUtils.isNotBlank(ecUserDto.getUserName())){
            wrapper.like("user_name",ecUserDto.getUserName());
        }
        List<EcUser> list = this.selectList(wrapper);
        PageInfo<EcUser> pageInfo = new PageInfo(list);
        Map<Integer,List<ExcelBean>> map = new HashMap<Integer,List<ExcelBean>>();
        List<ExcelBean> excelBeans = new ArrayList<>();
        excelBeans.add(new ExcelBean("id","id",0));
        excelBeans.add(new ExcelBean("备注","bz",0));
        excelBeans.add(new ExcelBean("密码","password",0));
        excelBeans.add(new ExcelBean("真实姓名","trueName",0));
        excelBeans.add(new ExcelBean("用户名","userName",0));
        excelBeans.add(new ExcelBean("是否删除","deleted",0));
        map.put(0,excelBeans);

        return ExcelUtils.createExcelFile(EcUser.class,pageInfo.getList(),map,"用户列表");
    }

    @Override
    public void uplodExcel(MultipartFile multipartFile) throws Exception {
        List<List<Object>> bankListByExcel = ExcelUtils.getBankListByExcel(multipartFile.getInputStream(), multipartFile.getOriginalFilename());
        List<EcUser> list = new ArrayList<>();
        for (List<Object> objects : bankListByExcel) {
            EcUser ecUser = new EcUser();
            ecUser.setBz(objects.get(1).toString());
            ecUser.setPassword(objects.get(2).toString());
            ecUser.setTrueName(objects.get(3).toString());
            ecUser.setUserName(objects.get(4).toString());
            ecUser.setDeleted(Integer.valueOf(objects.get(5).toString()));
            list.add(ecUser);
        }
        this.insertBatch(list);
    }
}
